package com.franzliszt.OrderOnline.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.franzliszt.OrderOnline.tool.GoodsList;
import com.franzliszt.hospitalnumberingsystem.R;


import java.util.List;

public class goodsListAdapter extends BaseQuickAdapter<GoodsList, BaseViewHolder> {
    private Context context;
    private double totalPrice = 0;
    public goodsListAdapter(Context context,int layoutResId, @Nullable List<GoodsList> data) {
        super( layoutResId, data );
        this.context = context;
    }

    public goodsListAdapter(@Nullable List<GoodsList> data) {
        super( data );
    }

    public goodsListAdapter(int layoutResId) {
        super( layoutResId );
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsList item) {
        Drawable drawable = context.getResources().getDrawable( item.getImg() );
        Drawable drawableSub = context.getResources().getDrawable( item.getSub() );
        Drawable drawableAdd = context.getResources().getDrawable( item.getAdd() );
        helper.setImageDrawable( R.id.goodsImg,drawable );
        helper.setText( R.id.goodsName,item.getName() );
        helper.setText( R.id.goodsMoney,item.getPrice()+"" );
        helper.setText( R.id.goodsNum,item.getNumber()+"" );
        helper.setImageDrawable( R.id.goodsSub,drawableSub );
        helper.setImageDrawable( R.id.goodsAdd,drawableAdd );
        helper.addOnClickListener( R.id.goodsAdd );
        helper.addOnClickListener( R.id.goodsSub );
    }
    public void removeAllItem(List<GoodsList> item){
        item.removeAll( item );
        notifyDataSetChanged();
    }
    /*传入数值，获取金钱，然后获取number,返回总金额*/
    public void addPrice(List<GoodsList> item,double price,int position,int number){
        totalPrice += price;
        number++;
        item.get( position ).setNumber( number );
        notifyDataSetChanged();
    }
    public void subPrice(List<GoodsList> item,double price,int position,int number){
        number--;
        if (number > 0){
            totalPrice -= price;
             item.get( position ).setNumber( number );
        }else {
           item.remove( position );
        }
        notifyDataSetChanged();
    }
    public double TotalPrice(List<GoodsList> item){
        double totalPrice = 0;
        for (int i = 0; i < item.size(); i++) {
            int num = item.get( i ).getNumber();
            double price = item.get( i ).getPrice();
            totalPrice += num * price;
        }
        return totalPrice;
    }
    public int TotalNumber(List<GoodsList> item){
        int num = 0;
        for (int i = 0; i <item.size() ; i++) {
            num += item.get( i ).getNumber();
        }
        return num;
    }
}
